package com.level2app.ui.app;

import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;

import com.level2app.R;
import com.level2app.base.BaseActivity;
import com.level2app.base.BaseViewModel;
import com.level2app.databinding.ActivityMainBinding;
import com.level2app.utils.FragmentUtils;
import com.level2app.utils.ToastHelper;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity<ActivityMainBinding, BaseViewModel> {

    private final List<Fragment> mFragments = new ArrayList<>();
    private final List<Integer> mTabs = new ArrayList<>();

    @Override
    protected BaseViewModel bindModel() {
        return getViewModel(this, BaseViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        mFragments.add(new HomeFragment());
        mFragments.add(new NetworkFragment());
        mFragments.add(new ReservationsFragment());
        mFragments.add(new WalletFragment());
        mFragments.add(new AccountFragment());
        mTabs.add(R.id.tab1);
        mTabs.add(R.id.tab2);
        mTabs.add(R.id.tab3);
        mTabs.add(R.id.tab4);
        mTabs.add(R.id.tab5);
        binding.radioGroup.setOnCheckedChangeListener(mOnCheckedChangeListener);
        FragmentUtils.add(getSupportFragmentManager(), mFragments, R.id.frameLayout, 0);
    }

    private final RadioGroup.OnCheckedChangeListener mOnCheckedChangeListener = (group, checkedId) -> {
//        binding.viewPager.setCurrentItem(mTabs.indexOf(checkedId));
        if (checkedId == R.id.tab1) {
            binding.tab1.setText(getString(R.string.home));
            binding.tab2.setText("");
            binding.tab3.setText("");
            binding.tab4.setText("");
            binding.tab5.setText("");
            FragmentUtils.showHide(0, mFragments);
        } else if (checkedId == R.id.tab2) {
            binding.tab2.setText(getString(R.string.network));
            binding.tab1.setText("");
            binding.tab3.setText("");
            binding.tab4.setText("");
            binding.tab5.setText("");
            FragmentUtils.showHide(1, mFragments);
        } else if (checkedId == R.id.tab3) {
            binding.tab3.setText(getString(R.string.reservations));
            binding.tab1.setText("");
            binding.tab2.setText("");
            binding.tab4.setText("");
            binding.tab5.setText("");
            FragmentUtils.showHide(2, mFragments);
        } else if (checkedId == R.id.tab4) {
            binding.tab4.setText(getString(R.string.wallet));
            binding.tab1.setText("");
            binding.tab2.setText("");
            binding.tab3.setText("");
            binding.tab5.setText("");
            FragmentUtils.showHide(3, mFragments);
        } else {
            binding.tab5.setText(getString(R.string.account));
            binding.tab1.setText("");
            binding.tab2.setText("");
            binding.tab3.setText("");
            binding.tab4.setText("");
            FragmentUtils.showHide(4, mFragments);
        }
    };

    @Override
    protected void initClick() {
        super.initClick();
//        binding.viewPager.setOnTouchListener((view, motionEvent) -> true);
    }

    // 再点一次退出程序时间设置
    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;
    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
            super.onBackPressed();
            finish();
        } else {
            TOUCH_TIME = System.currentTimeMillis();
            ToastHelper.showToast("再次返回退出Level 2");
        }
    }
}